
// Jquery

var id = 1;

function sel() {
	var name = $("[name='name2']").val();
	var addr = $("select[name='addr_s']").val();
	$("#ttbody tr").css("background","");
	if (name == '' && addr == '') return;
	$("#ttbody tr").each(function(index){
		var n = $(this).find('td:eq(1)').text();
		var a = $(this).find('td:eq(4)').text();
		var v1 = (name==''||n.indexOf(name)>-1);
		var v2 = (addr==''||a.indexOf(addr)>-1);
		if(v1&&v2){
			$(this).css("background","red");
		}
	});

}

function add() {
	var name = $("[name='name']")[0].value;
	var sex_val = $("input[name='sex']:checked").val();
	var hobby_val = "";
	$("input[name='hobby']:checked").each(function(){
		hobby_val += ($(this).val()+"")
	});
	var addr = $("select[name='addr']").val();

	var html = "<tr id='tr" + id + "'>" +
		"<td id='td1" + id + "'>" + id + "</td>" +
		"<td id='td2" + id + "'>" + name + "</td>" +
		"<td id='td3" + id + "'>" + sex_val + "</td>" +
		"<td id='td4" + id + "'>" + hobby_val + "</td>" +
		"<td id='td5" + id + "'>" + addr + "</td>" +
		"<td id='td6" + id + "'>" + "<a href='javascript:del(" + id + ")'>删除</a>  <a href='javascript:upd(" + id +
		")'>修改</a>" + "</td>" +
		"</tr>";

	$("#ttbody").append(html);
	id++;

}


function del(id){
	if(confirm("是否删除该记录？")){
		$("#tr"+id).remove();
	}
}

function upd(id){
	var td2 = $("#td2"+id);
	var td3 = $("#td3"+id);
	var td4 = $("#td4"+id);
	var td5 = $("#td5"+id);
	var td6 = $("#td6"+id);
	var name = td2.text();
	var sex = td3.text();
	var hobby = td4.text();
	var addr = td5.text();
	
	td2.html("<input type=\"text\" name=\"upd_name" + id + "\" value=\"" + name + "\" />");
	
	var isMan = (sex == "男") ? "checked" : "";
	var isWoman = (sex == "女") ? "checked" : "";
	td3.html("<label><input type=\"radio\" name=\"upd_sex" + id + "\" value=\"男\" " + isMan + "/>男</label>" +
		"<label><input type=\"radio\" name=\"upd_sex" + id + "\" value=\"女\" " + isWoman + "/>女</label>");
	
	var isRun = (hobby.indexOf('跑步') > -1) ? "checked" : "";
	var isSwim = (hobby.indexOf('游泳') > -1) ? "checked" : "";
	var isBad = (hobby.indexOf('羽毛球') > -1) ? "checked" : "";
	td4.html("<label><input type='checkbox' name=\"upd_hobby" + id + "\" value='跑步' " + isRun + "/>跑步</label>" +
		"<label><input type='checkbox' name=\"upd_hobby" + id + "\" value='游泳'  " + isSwim + "/>游泳</label>" +
		"<label><input type='checkbox' name=\"upd_hobby" + id + "\" value='羽毛球'  " + isBad + "/>羽毛球</label>");
	
	var isBj = (addr == '北京') ? "selected" : "";
	var isSh = (addr == '上海') ? "selected" : "";
	var isSz = (addr == '深圳') ? "selected" : "";
	td5.html("<select name=\"upd_addr" + id + "\">" +
		"<option value='北京' " + isBj + " >北京</option>" +
		"<option value='上海'" + isSh + ">上海</option>" +
		"<option value='深圳'" + isSz + ">深圳</option>" +
		"</select>");
	
	td6.html("<a href ='javascript:save(" + id + ");'>保存</a>");
	
}

function save(id) {
	var name = $("[name='upd_name"+id+"']").val();
	var sex_val = $("input[name='upd_sex"+id+"']:checked").val();
	var hobby_val = "";
	$("input[name='upd_hobby"+id+"']:checked").each(function(){
		hobby_val += ($(this).val()+"")
	});
	var addr = $("select[name='upd_addr"+id+"']").val();

	$("#td2" + id).html(name);
	$("#td3" + id).html(sex_val);
	$("#td4" + id).html(hobby_val);
	$("#td5" + id).html(addr);
	$("#td6" + id).html("<a href='javascript:del(" + id +
		")'>删除</a>  <a href='javascript:upd(" + id +
		")'>修改</a>");
	
}

